<?php
/**
 * Banners_model Class
 * @package TIAP
 * @subpackage Model
 * @category banners
 * @author Suraj Maharjan
 * @link http://twitter.com/
 */
class Packages_model extends MY_Model{
	function Packages_model(){
            parent::MY_Model();
            $this->user_package_relation_table = 'directory_package_relationships';
            $this->package_feature_table = 'package_features';
            $this->package_offer_table = 'package_offers';
	}
    
    function update_directory_package_list($user_id, $package_id){
        $table = $this->user_package_relation_table;
        $table_offer = $this->package_offer_table;
        $query = $this->db->get_where($table_offer,array('id'=>$package_id));
        $package_offer_detail = $query->row_array();
        $tempArray = explode(" ",$package_offer_detail['offer_name']);
        $time = $tempArray[0];
        if($time<30){
             $mktime = mktime(0, 0, 0, date('m')+(int)$time, date('d'), date('Y'));
        }else{
            $mktime = mktime(0, 0, 0, date('m'), date('d')+(int)$time, date('Y'));
        }
        //debug($package_offer_detail);exit;
//        $sqlDelete = "DELETE FROM {$table} WHERE `directory_user_id` = {$user_id}  AND `package_feature_id` = {$package_feature_id}";
//        $this->db->query($sqlDelete);
        $issue_date = date("Y-m-d H:i:s");
        $expire_date = date("Y-m-d H:i:s",$mktime);
        $array_insert = array(
            'directory_user_id'=>$user_id,
            'package_offer_id'=>$package_id,
            'issue_date'=>$issue_date,
            'expire_date'=>$expire_date,
            'status'=>1,
        );
        $status = $this->db->insert($table, $array_insert);
        return ($status)?true:false;
    }

    function get_package_features($package_type_id){
        $table = $this->package_feature_table;
        $this->db->from($table);
        $this->db->where(array('package_type_id' => $package_type_id ));
        $this->db->order_by('id','asc');

        $query = $this->db->get();
         if ($query->num_rows() > 0) {
        $result = $query->result_array();
            return $result;
        } else {
            return false;
        }

    }
    function get_package_detail_by_user_id($userid=null){
        //echo "__$userid";exit;
        if($userid==null) return false;
        $sql = "SELECT du.email, dc.company,dpr.issue_date,dpr.expire_date,po.offer_name,po.currency_type,po.cost,po.special,pt.name,pt.membership_name from directory_user as du ";
        $sql .= "join directory_company as dc on du.id = dc.id ";
        $sql .= "join directory_package_relationships as dpr on du.id = dpr.directory_user_id ";
        $sql .= "join package_offers as po on dpr.package_offer_id=po.id ";
        $sql .= "join package_types as pt on pt.id = po.package_type_id ";
        $sql .= "where du.id = $userid";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            return $result;
            //return $this->db->last_query();
        } else {
            return false;
        }
    }
     
  
       
}